home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d26 / inf_src.arc / INFERENC.C < prev    next >
C/C++ Source or Header  |  1986-03-14  |  5KB  |  211 lines

  1. /*****************************************************************
  2. **                                **
  3. **      Inference -- (C) Copyright 1985 George Hageman    **
  4. **                                **
  5. **        user-supported software:                **
  6. **                                **
  7. **            George Hageman                **
  8. **            P.O. Box 11234                **
  9. **            Boulder, Colorado 80302            **
  10. **                                **
  11. *****************************************************************/
  12.  
  13. /*******************************************************************
  14. **
  15. **    inference engine main routine
  16. **
  17. *******************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #include "expert.h"
  23. #include "routine.h"
  24.  
  25. #define    MAX_KNOWN    100
  26.  
  27. int    numHypot, hypStack[MAX_HYPS],strBuffOfst ;
  28. char    strBuff[MAX_STRING_BUFF] ;
  29. int    ruleBuffOfst ;
  30. int    knownTrue[MAX_KNOWN], knownFalse[MAX_KNOWN] ;
  31. int    numTrue, numFalse ;
  32. struct  rule_statement_r ruleBuff[MAX_RULE_STATEMENTS] ;
  33.  
  34. int    main(argc,argv)
  35. int argc ;
  36. char **argv ;
  37.  
  38. {
  39. int    i,consequent ;
  40. int    proved ;
  41. int    p_value ;
  42. FILE    *infile ;
  43.  
  44. for( proved = 0; proved < MAX_STRING_BUFF; proved ++ )
  45.     strBuff[proved] = 0 ;
  46.  
  47. proved = FALSE ;
  48.  
  49. #ifdef    DEBUG
  50.     fprintf(stdout,"\nDEBUG-RULECOMP argc=%d ",argc) ;
  51. #endif
  52.  
  53. if(argc != 2 )
  54.     {
  55.     fprintf(stdout, "\n\n Usage: inference in.file \n " ) ;
  56.     exit(RETURN_ROUTINE_FALSE) ;
  57.     }
  58. infile = fopen( argv[1], "rb" );
  59. if(infile == NULL)
  60.     {
  61.     fprintf(stdout,"\n\n Cannot open input file %s \n ", argv[1]);
  62.     exit(RETURN_ROUTINE_FALSE) ;
  63.     }
  64. for(i=0;i<MAX_KNOWN;i++)
  65.     knownTrue[i]=knownFalse[i]=0 ;
  66. /*
  67. **
  68. **    read in the compiled rules
  69. **
  70. */
  71.  
  72. fread(&numHypot,sizeof(int),1,infile) ;
  73. fread(hypStack,sizeof(int),numHypot,infile) ;
  74. fread(&ruleBuffOfst,sizeof(int),1,infile) ;
  75. fread(ruleBuff,2*sizeof(int),ruleBuffOfst,infile) ;
  76. fread(&strBuffOfst,sizeof(int),1,infile) ;
  77. fread(strBuff,1,strBuffOfst,infile) ;
  78.  
  79. #ifdef    DEBUG
  80.     
  81. printf("\nDEBUG-main, numHypot = %d",numHypot) ;
  82. for(i=0;i<numHypot;i++)
  83.     {
  84.     printf("\nDEBUG-main, consequent[%d]= %d, string=%s",i,hypStack[i],
  85.             &strBuff[(ruleBuff[hypStack[i]].string)] ) ;
  86.     }
  87. printf("\nDEBUG-main, number of rules = %d",ruleBuffOfst) ;
  88. printf("\nDEBUG-main, number of bytes in stringbuffer=%d",strBuffOfst) ;
  89. for(i=0;i<ruleBuffOfst;i++)
  90.     {
  91.     if(ruleBuff[i].flag !=0)
  92.         {
  93.         printf("\nDEBUG-main, rule[%d].flag=%d, rule[%d].string= %d->%s"
  94.                   ,i,ruleBuff[i].flag,i,ruleBuff[i].string,&strBuff[ruleBuff[i].string]) ;
  95.         }
  96.     }
  97. printf("\n") ;
  98.  
  99. #endif
  100.  
  101. for( i=0 ; i < numHypot ; i++ )
  102.  
  103.     {
  104.     consequent = hypStack[i] ;
  105.  
  106. #ifdef DEBUG
  107.     printf("\nDEBUG-main, consequent = %d ",consequent ) ;
  108. #endif
  109.  
  110.     if(weKnow(consequent,&p_value) == TRUE)
  111.         {
  112. #ifdef DEBUG
  113.     printf("\nDEBUG-main, we knew this consequent was ") ;
  114.     if(p_value == TRUE)
  115.         printf("-- TRUE") ;
  116.     else
  117.         printf("-- FALSE") ;
  118. #endif
  119.         continue ;
  120.         }
  121.         
  122.     if ( verify(consequent) == TRUE )
  123.         {
  124.         
  125. #ifdef DEBUG
  126.             printf("\nDEBUG-main, consequent verified TRUE " ) ;
  127. #endif
  128.         
  129.         if( (ruleBuff[consequent].flag == ROUTINE_TRUE) ||
  130.             (ruleBuff[consequent].flag == ROUTINE_TRUE_HYP) )
  131.             {
  132.             if(weKnow(consequent,&p_value) == TRUE)
  133.                 continue ;
  134. #ifdef DEBUG
  135.             printf("\nRuning Routine %s",&strBuff[ruleBuff[consequent].string]) ;
  136. #endif
  137.             if (runRoutine(consequent) == TRUE)
  138.                 {
  139.                 knownTrue[numTrue++]=ruleBuff[consequent].string ;
  140. #ifdef DEBUG
  141.                 printf(" -- TRUE\n") ;
  142. #endif
  143.                 if(ruleBuff[consequent].flag == ROUTINE_TRUE_HYP)
  144.                     {
  145.                     printf("\nCONCLUSION\n") ;
  146.                     exit(RETURN_ROUTINE_TRUE) ;
  147.                     }
  148.                 }
  149.             else
  150.                 {
  151.                 knownFalse[numFalse++]=ruleBuff[consequent].string ;
  152. #ifdef DEBUG
  153.                 printf(" -- FALSE\n") ;
  154. #endif
  155.                 }
  156.             }
  157.         else
  158.             {
  159.             knownTrue[numTrue++]=ruleBuff[consequent].string ;
  160.             proved = TRUE ;
  161.             printf("\nI infer that : %s\n",&strBuff[ruleBuff[consequent].string]) ;
  162.             if(ruleBuff[consequent].flag == STRING_TRUE_HYP)
  163.                 {
  164.                 printf("\nCONCLUSION\n") ;
  165.                 exit(RETURN_ROUTINE_TRUE) ;
  166.                 }
  167.             }
  168.         }
  169.     else
  170.         {
  171. #ifdef DEBUG
  172.             printf("\nDEBUG-main, consequent not proved " ) ;
  173. #endif
  174.         }
  175.     }
  176. if(proved == FALSE)
  177.     {
  178.     printf("\n I can't prove anything\n") ;
  179.     exit(RETURN_ROUTINE_FALSE) ;
  180.     }
  181. exit(RETURN_ROUTINE_TRUE) ;
  182. }
  183.  
  184. /* - -<<hidden tof */
  185.  
  186. /******************************************************
  187. **
  188. **    known(hypot,knownFile,numKnown)
  189. **
  190. **    checks to see if hypot is in the stack knownFile of length
  191. **        numKnown
  192. **
  193. **    returns true if known, false otherwise
  194. **
  195. ******************************************************/
  196.  
  197.  
  198.  
  199. known(hypot,knownFile,numKnown)
  200.     int     hypot,numKnown ;
  201.     int    knownFile[] ;
  202. {
  203. int i ;
  204. for (i = 0 ; i < numKnown ; i++ )
  205.     if (ruleBuff[hypot].string == knownFile[i] )
  206.         return(TRUE) ;
  207. return(FALSE) ;
  208. }
  209.  
  210.  
  211.